/**
 * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springblade.modules.store.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import org.springblade.common.constant.PostIdConstant;
import org.springblade.common.constant.RoleIdConstant;
import org.springblade.modules.store.entity.Store;
import org.springblade.modules.store.vo.StoreVO;
import org.springblade.modules.store.mapper.StoreMapper;
import org.springblade.modules.store.service.IStoreService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.mapper.UserMapper;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.List;

/**
 * 商店表 服务实现类
 *
 * @author Blade
 * @since 2021-01-23
 */
@Service
@AllArgsConstructor
public class StoreServiceImpl extends BaseServiceImpl<StoreMapper, Store> implements IStoreService {

	private UserMapper userMapper;

	public String isApproval(String type){
		String name;
		switch (type){
			case "1":
				name = "通过";
			break;
			case "2":
				name = "未通过";
				break;
			default:
				name = "未审批";
		}
		return name;
	}

	@Override
	public IPage<StoreVO> selectStorePage(IPage<StoreVO> page, StoreVO store) {
		List<StoreVO> storeVOS = baseMapper.selectStorePage(page, store);
		for (StoreVO storeVO : storeVOS) {
			String type = isApproval(storeVO.getApproval());
			storeVO.setIsApproval(type);
		}
		return page.setRecords(storeVOS);
	}

	@Override
	public boolean updateApproval(Store store) {
		User user = userMapper.selectById(store.getLoginId());
		if (store.getApproval().equals("1")){
			user.setRoleId(RoleIdConstant.STORE_ROLE);
			user.setPostId(PostIdConstant.STORE_POST);
			user.setAvatar(store.getImg());
			user.setName(store.getStoreName());
			user.setRealName(store.getStoreName());
			userMapper.updateById(user);
			baseMapper.updateById(store);
		}else {
			baseMapper.updateById(store);
		}
		return true;
	}

}
